package com.ruoyi.publish.service.impl;

import java.util.List;

import com.ruoyi.publish.constant.ReviewConstant;
import com.ruoyi.publish.domain.RvSample;
import com.ruoyi.publish.mapper.RvSampleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.publish.mapper.RvReviewMapper;
import com.ruoyi.publish.domain.RvReview;
import com.ruoyi.publish.service.IRvReviewService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 审读稿件Service业务层处理
 *
 * @author confusefox
 * @date 2020-11-01
 */
@Service
public class RvReviewServiceImpl implements IRvReviewService {
    @Autowired
    private RvReviewMapper rvReviewMapper;
    @Autowired
    private RvSampleMapper rvSampleMapper;

    /**
     * 查询审读稿件
     *
     * @param id 审读稿件ID
     * @return 审读稿件
     */
    @Override
    public RvReview selectRvReviewById(Long id) {
        return rvReviewMapper.selectRvReviewById(id);
    }

    /**
     * 查询审读稿件列表
     *
     * @param rvReview 审读稿件
     * @return 审读稿件
     */
    @Override
    public List<RvReview> selectRvReviewList(RvReview rvReview) {
        return rvReviewMapper.selectRvReviewList(rvReview);
    }

    /**
     * 新增审读稿件，稿件赋值出版单位，缴送时间； 改变样本的review_id
     *
     * @param rvReview 审读稿件
     * @return 结果
     */
    @Override
    @Transactional
    public int insertRvReview(RvReview rvReview) {
        int reslult;
        RvSample rvSample = rvSampleMapper.selectRvSampleById(rvReview.getSampleid());
        Assert.isTrue(rvSample.getReviewId().equals(ReviewConstant.REVIEW_UNREAD),//
                "关联审读样本必须为未读");
        sampleToReview(rvReview, rvSample);
        reslult = rvReviewMapper.insertRvReview(rvReview);
        rvSample.setReviewId(rvReview.getId());
        rvSampleMapper.updateRvSample(rvSample);
        return reslult;
    }

    /**
     * 样本的出版单位，缴送时间 赋值 给 审读稿件
     *
     * @param rvReview
     * @param rvSample
     */
    private void sampleToReview(RvReview rvReview, RvSample rvSample) {
        rvReview.setPublisherId(rvSample.getPublisherId());
        rvReview.setPublisher(rvSample.getPublisher());
        rvReview.setDeliveryTime(rvSample.getDeliveryTime());
    }

    /**
     * 修改审读稿件， 原样本改为未审读
     *
     * @param rvReview 审读稿件
     * @return 结果
     */
    @Override
    @Transactional
    public int updateRvReview(RvReview rvReview) {
        int reslult;
        // 新传入的样本
        RvSample rvSample = rvSampleMapper.selectRvSampleById(rvReview.getSampleid());
        //原稿件
        RvReview oldRvReview = rvReviewMapper.selectRvReviewById(rvReview.getId());

        // 样本一样直接更新
        if (rvReview.getSampleid() != null && rvReview.getSampleid().equals(oldRvReview.getSampleid())) {
            return rvReviewMapper.updateRvReview(rvReview);
        }

        // 样本改变
        Assert.isTrue(rvSample.getReviewId().equals(ReviewConstant.REVIEW_UNREAD),//
                "关联审读样本必须为未读");
        sampleToReview(rvReview, rvSample);
        reslult = rvReviewMapper.insertRvReview(rvReview);
        rvSample.setReviewId(rvReview.getId());
        rvSampleMapper.updateRvSample(rvSample);
        RvSample oldRvSample = rvSampleMapper.selectRvSampleById(oldRvReview.getSampleid());
        if (oldRvSample != null) {
            oldRvSample.setReviewId(ReviewConstant.REVIEW_UNREAD);
            rvSampleMapper.updateRvSample(oldRvSample);
        }

        return reslult;
    }

    /**
     * 删除审读稿件对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteRvReviewByIds(String ids) {
        String[] arrid = Convert.toStrArray(ids);
        for (String s : arrid) {
            Long id = Long.valueOf(s);
            RvReview rvReview = rvReviewMapper.selectRvReviewById(id);
            if (rvReview != null) {
                Long sampleid = rvReview.getSampleid();
                RvSample rvSample = rvSampleMapper.selectRvSampleById(sampleid);
                rvSample.setReviewId(ReviewConstant.REVIEW_UNREAD);
                rvSampleMapper.updateRvSample(rvSample);
            }
        }

        return rvReviewMapper.deleteRvReviewByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除审读稿件信息，关联样本 reviewid清零
     *
     * @param id 审读稿件ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteRvReviewById(Long id) {
        RvReview rvReview = rvReviewMapper.selectRvReviewById(id);
        if (rvReview != null) {
            Long sampleid = rvReview.getSampleid();
            RvSample rvSample = rvSampleMapper.selectRvSampleById(sampleid);
            rvSample.setReviewId(ReviewConstant.REVIEW_UNREAD);
            rvSampleMapper.updateRvSample(rvSample);
        }
        return rvReviewMapper.deleteRvReviewById(id);
    }

    /**
     * 根据Id获取数据
     *
     * @param ids
     * @return
     */
    @Override
    public List<RvReview> selectRecordByIds(String[] ids) {
        return rvReviewMapper.selectRecordByIds(ids);
    }
}
